/**
 * Filename: AlbumMemberFragment.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/6 14:45
 */
package com.github.postsanf.yinian.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.BaseFragment;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.AlbumMemberAdapter;
import com.github.postsanf.yinian.bean.YNMember;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.service.AlbumContentService;
import com.github.postsanf.yinian.utils.WrapContentGridLayoutManager;
import com.github.postsanf.yinian.view.SpacesItemDecoration;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class AlbumMemberFragment extends BaseFragment {
    private View view;                       // 上下文视图
    private RecyclerView rv_home_album;              // 列表视图
    private AlbumMemberAdapter mAdapter;                   // 适配器
    private List<YNMember> mMemberDatas;               // 成员数据

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 动态绑定广播
        registerBoradcastReceiver();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mMemberDatas = new ArrayList<>();
        noticeServiceInit();
        initView();
        return view;
    }

    private void initView() {
        view = View.inflate(getActivity(), R.layout.yn_fragment_album, null);
        rv_home_album = (RecyclerView) view.findViewById(R.id.rv_home_album);
        //设置布局管理器
        rv_home_album.setLayoutManager(new WrapContentGridLayoutManager(getActivity(), 4));
        // 设置适配器
        mAdapter = new AlbumMemberAdapter(getActivity(), mMemberDatas, YNSingleton.getInstance().getCurAlbum(), false, application.getCurID() + "");
        rv_home_album.setAdapter(mAdapter);
        rv_home_album.setHasFixedSize(true);
        rv_home_album.setItemViewCacheSize(50);
        //设置item之间的间隔
        SpacesItemDecoration decoration = new SpacesItemDecoration(4);
        rv_home_album.addItemDecoration(decoration);

    }

    // 更新成员数据
    private void updateView(List<YNMember> memberList) {
        mMemberDatas.addAll(memberList);
        mAdapter.notifyDataSetChanged();
    }

    // 分析并执行动作
    BroadcastReceiver mMemberBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                //　加载成员数据
                case ACTIONs.actionMemberUpdate:
                    List<YNMember> memberList = (List<YNMember>) intent.getSerializableExtra(ACTIONs.aAddData);
                    updateView(memberList);
                    break;
            }
        }
    };

    // 注册接收者
    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionMemberUpdate);
        getActivity().registerReceiver(mMemberBroadcastReceiver, myIntentFilter);
    }

    // 通知后台加载数据
    private void noticeServiceInit() {
        if (getActivity() == null) {
            return;
        }
        Intent mIntent = new Intent(getActivity(), AlbumContentService.class);
        mIntent.setAction(ACTIONs.actionAlbumMember);
        mIntent.putExtra(ACTIONs.aGroupId, application.getCurGid());
        getActivity().startService(mIntent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        BaseApplication.getInstance().mActivitys.remove(this);
        getActivity().unregisterReceiver(mMemberBroadcastReceiver);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainScreen");
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
    }
}